package com.cmxh.shipper.controller;

import com.cmxh.shipper.pojo.Exclusivefleet;
import com.cmxh.shipper.pojo.Freightsources;
import com.cmxh.shipper.pojo.Team;
import com.cmxh.shipper.service.ExclusivefleetService;
import com.cmxh.shipper.service.FreightsourcesService;
import com.cmxh.shipper.util.Code;
import com.cmxh.shipper.util.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 *
 *货源信息Controller
 * */
@RestController
public class Freightsourcescontroller {
    @Autowired
    private FreightsourcesService freightsourcesService;
    @Autowired
    private ExclusivefleetService exclusivefleetService;

    //查询所有货源信息根据货源状态
    @RequestMapping("/selectHuoYuanBYStatus")
    public Result selectHuoYuanBYStatus(@RequestBody Freightsources freightsourcess) {
        List<Freightsources> freightsources = freightsourcesService.selectHuoYuanBYStatus(freightsourcess);
        if (freightsources != null) {
            return new Result(Code.GET_OK, freightsources, "查询成功");
        } else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }

    //查询所有货源信息
    @RequestMapping("/selectHuoYuanq/{dirverid}")
    public Result selectHuoYuanq(@PathVariable Integer dirverid) {
        List<Freightsources> xx = freightsourcesService.selectHuoYuan(dirverid);
        if (xx != null) {
            return new Result(Code.GET_OK, xx, "查询成功");
        } else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }

    @RequestMapping("/addfreightsources")
    public Result addfreightsources(@RequestBody Map<String, Object> freightsources) {
        try {
            // 将 Map 中的 duixiang 转为 Freightsources 对象
            ObjectMapper mapper = new ObjectMapper();
            Freightsources o = mapper.convertValue(freightsources.get("duixiang"), Freightsources.class);

            int insert = freightsourcesService.insert(o);
            if (insert > 0) {
                Integer newid = o.getSourceid(); // 拿到数据库生成的ID

                // 判断是否传递了 car 数据
                if (freightsources.containsKey("car")) {
                    // 将 car 列表转为 List<Team>
                    List<Team> exclusivefleets = mapper.convertValue(
                            freightsources.get("car"),
                            mapper.getTypeFactory().constructCollectionType(List.class, Team.class)
                    );

                    if (exclusivefleets == null || exclusivefleets.isEmpty()) {
                        return new Result(Code.SAVE_OK, newid,null);
                    } else {
                        List<Exclusivefleet> teams = new ArrayList<>();
                        for (Team team : exclusivefleets) {
                            Exclusivefleet exclusivefleet = new Exclusivefleet();
                            exclusivefleet.setSourceid(newid);
                            exclusivefleet.setTid(team.getId());
                            teams.add(exclusivefleet);
                        }
                        int i = exclusivefleetService.insertAll(teams);
                        if (i > 0) {
                            return new Result(Code.SAVE_OK,newid, "添加成功");
                        } else {
                            return new Result(Code.SAVE_ERR,newid, "车队关联失败");
                        }
                    }
                } else {
                    // 如果没有传车队数据，直接返回成功
                    return new Result(Code.SAVE_OK, newid,"添加成功，未选择车队");
                }
            } else {
                return new Result(Code.SAVE_ERR, "添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Code.SAVE_ERR, "系统异常：" + e.getMessage());
        }
    }



    @RequestMapping("/selectHuoYuanw/{sourceid}")
    public Result selectHuoYuanw(@PathVariable Integer sourceid) {
        Freightsources xx = freightsourcesService.selectHuoYuanw(sourceid);
        if (xx != null) {
            return new Result(Code.GET_OK, xx, "查询成功");
        } else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }
    @RequestMapping("/updates/{sourceid}")
    public Result updates(@PathVariable Integer sourceid) {
        int i = freightsourcesService.updates(sourceid);
        if (i > 0) {
            return new Result(Code.UPDATE_OK, "修改成功");
        } else {
            return new Result(Code.UPDATE_ERR, "修改失败");
        }
    }



}